package com.haoran.dwz.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {

	@Value("${swagger.scan-package:com.haoran.dwz.controller}")
	private String scanPackage;

	@Value("${swagger.title:短网址接口}")
	private String title;

	@Value("${swagger.description:RESTful API Of DWZ}")
	private String description;

	@Value("${swagger.termsOfServiceUrl:#}")
	private String termsOfServiceUrl;

	@Value("${swagger.version:1.0.0}")
	private String version;

	@Value("${swagger.contact.name:haoran_wen}")
	private String name;

	@Value("${swagger.contact.email:3429293919@qq.com}")
	private String email;

	@Bean
	public Contact createContact() {
		Contact contact = new Contact(name, "", "");
		return contact;
	}

	@Bean
	public Docket createRestApi(@Autowired Contact contact) {
		return new Docket(DocumentationType.SWAGGER_2).useDefaultResponseMessages(false).apiInfo(apiInfo(contact))
				.select().apis(RequestHandlerSelectors.basePackage(scanPackage)).paths(PathSelectors.any()).build();
	}

	private ApiInfo apiInfo(Contact contact) {
		return new ApiInfoBuilder().title(title).description(description).termsOfServiceUrl(termsOfServiceUrl)
				.contact(contact).version(version).build();
	}

}
